package com.baitiaojun.security.core.handler;

import com.baitiaojun.common.constants.HttpStatus;
import com.baitiaojun.common.domain.model.result.CommonResult;
import com.baitiaojun.common.enums.impl.ServerExpEnums;
import com.baitiaojun.common.enums.impl.ServiceExpEnums;
import com.baitiaojun.common.utils.servlet.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token认证失败处理器
 */
@Slf4j
@Component
public class AuthFailureHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.warn(authException.getMessage());
        ServerExpEnums expLoginUser = ServerExpEnums.EXP_LOGIN_USER;
        CommonResult error = CommonResult.error(expLoginUser.getCode(), expLoginUser.getName());
        ServletUtils.render(response, error);
    }
}
